package com.fxhy.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baby.myutils.DateUtils;
import com.fxhy.bean.AgentLog;
import com.fxhy.bean.AgentUser;
import com.fxhy.mapper.AgentLogMapper;
import com.fxhy.repository.AgentLogRepository;
import com.fxhy.utils.MyDateUtil;

@Service
public class AgentLogService {
	@Autowired
	private AgentLogRepository repository;
	
	@Autowired
	private AgentLogMapper mapper;
	
	public List<AgentLog> findAgentLogListByUserId(Integer userId){
		Map<String,Object> map =  new HashMap<String,Object>();
		map.put("userId", userId);
		return mapper.findAgentLogListByMap(map);
	}
	
	public List<AgentLog> findAgentById(Integer userId){
		return repository.findByUserId(userId); 
	}
	
	public List<AgentLog> findAgentLogListByType(Integer userId,Integer type){
		Map<String,Object> map =  new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("type", type);
		return mapper.findAgentLogListByMap(map);
	}
	
	public AgentLog saveAgentLog(AgentLog log){
		return repository.saveAndFlush(log);
	}

	/**
	 * 登录日志
	 * @param userInfo 用户信息
	 * @param type 操作类型    1普通登录   2修改头像   3修改用户基础信息
	 * @param ip ip位置
	 * @return
	 */
	public AgentLog saveAgentLog(AgentUser userInfo, int type, String ip) {
		AgentLog log = new AgentLog();
		log.setUserId(userInfo.getUserId());
		log.setType(type);
		log.setTypeName("普通登录");
		log.setAction("用户【"+userInfo.getNickName()+"】于 [ "+MyDateUtil.getDateStrFormat("yyyy年MM月dd日  hh:mm:ss")+" ] 登录了系统!");
		log.setActionStatus(1);   //登陆成功
		log.setIp(ip);
		log.setStatus(1);
		log.setCreateTime(DateUtils.getNowDate());
		log.setCreateUser(userInfo.getNickName());
		return repository.saveAndFlush(log);
	}
	
	/**
	 * 保存日志
	 * @param userInfo 用户信息
	 * @param type 操作类型    1普通登录   2修改头像   3修改用户基础信息
	 * @return
	 */
	public AgentLog saveAgentLog(AgentUser userInfo, int type) {
		AgentLog log = new AgentLog();
		log.setUserId(userInfo.getUserId());
		log.setType(type);
		log.setTypeName(getTypeName(type));
		log.setAction(getAction(type, userInfo));
		log.setActionStatus(1);   //登陆成功
		log.setIp("不记录ip");
		log.setStatus(1);
		log.setCreateTime(DateUtils.getNowDate());
		log.setCreateUser(userInfo.getNickName());
		return repository.saveAndFlush(log);
	}

	private String getTypeName(int type) {
		switch (type) {
			case 1:
				return "普通登录";
			case 2:
				return "修改头像";
			case 3:
				return "修改用户基础信息";
			default:
				return "普通登录";
		}
	}
	
	private String getAction(int type,AgentUser userInfo) {
		StringBuffer sb = new StringBuffer();
		switch (type) {
			case 1:
				sb.append("用户【").append(userInfo.getNickName()+"】于 [ ").append(MyDateUtil.getDateStrFormat("yyyy年MM月dd日  hh:mm:ss")).append(" ] 登录了系统!");
				break;
			case 2:
				sb.append("用户【").append(userInfo.getNickName()+"】于 [ ").append(MyDateUtil.getDateStrFormat("yyyy年MM月dd日  hh:mm:ss")).append(" ] 修改了头像!");
				break;
			case 3:
				sb.append("用户【").append(userInfo.getNickName()+"】于 [ ").append(MyDateUtil.getDateStrFormat("yyyy年MM月dd日  hh:mm:ss")).append(" ] 修改了用户基础信息!");
				break;
			default:
				sb.append("用户【").append(userInfo.getNickName()+"】于 [ ").append(MyDateUtil.getDateStrFormat("yyyy年MM月dd日  hh:mm:ss")).append(" ] 登录了系统!");
				break;
		}
		return sb.toString();
	}
} 
